package com.peking.donations.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.peking.donations.plugins.oauth2.client.wechart.WechartOAuth2AccessTokenMessageConverter;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;

/**
 * httpClient 相关配置
 * 
 * Created by YGR on 16/3/8.
 */
@Configuration
public class RestConfiguration {

    /**
     * http 请求连接池
     * 
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "rest.client.pool")
    public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
        return new PoolingHttpClientConnectionManager();
    }

    /**
     * httpClientBuilder
     * 
     * @param clientConnectionManager
     * @return
     */
    @Bean
    public HttpClientBuilder httpClientBuilder(PoolingHttpClientConnectionManager clientConnectionManager) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(clientConnectionManager);
        return httpClientBuilder;
    }

    /**
     * httpClient
     * 
     * @param builder
     * @return
     */
    @Bean
    public CloseableHttpClient httpClient(HttpClientBuilder builder) {
        return builder.build();
    }

    /**
     * httpClient Request
     * 
     * @param httpClient
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "rest.client.request")
    public HttpComponentsClientHttpRequestFactory requestFactory(HttpClient httpClient) {
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }

    /**
     * restTemplate
     * 
     * @param requestFactory
     * @return
     */
    @Bean
    public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory requestFactory) {
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        List<HttpMessageConverter<?>> list = Lists.newArrayList();
        list.add(new WechartOAuth2AccessTokenMessageConverter());
        list.add(new ByteArrayHttpMessageConverter());
        list.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        list.add(new ResourceHttpMessageConverter());
        list.add(new SourceHttpMessageConverter());
        list.add(new AllEncompassingFormHttpMessageConverter());
        list.add(new FastJsonHttpMessageConverter());

        restTemplate.setMessageConverters(list);
        return restTemplate;
    }

}
